﻿using KillBill.Business.Entities;
using KillBill.Business.Entities.ListEntities;
using KillBill.Business.Modules.Products;
using KillBill.Services.DTO;
using KillBill.Services.Modules.Products.IServices;
using KillBill.Services.Modules.Products.SecurityServiceReference;

namespace KillBill.Services.Modules.Products
{
    public class ProductsService : IProductsService
    {
        private ProductsManager _productManager;
        private SecurityServiceClient _securityService;
        
        public ProductsService()
        {
            _productManager = new ProductsManager();
            _securityService = new SecurityServiceClient();
        }

        public ProductList GetProducts(string sessionId)
        {
            var list = new ProductList();
            list.DTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            if (list.Success)
                list = _productManager.GetProducts();
            return list;
        }

        public ProductList FindProducts(string value, string sessionId)
        {
            var list = new ProductList();
            list.DTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador" });
            if (list.Success)
                list = _productManager.FindProducts(value);
            return list;
        }

        public Product AddProduct(Product product, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador", "Bodeguero" });
            Product outDTO = new Product();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _productManager.AddProduct(product);
            return outDTO;
        }



        public Product UpdateProduct(Product product, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador", "Bodeguero" });
            Product outDTO = new Product();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _productManager.UpdateProduct(product);
            return outDTO;
        }


        public KillBillDTO DeleteProduct(Product product, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador", "Bodeguero" });
            KillBillDTO outDTO = new Product();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _productManager.DeleteProduct(product);
            return outDTO;
        }

        
        public ProductList GetProductList(string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador", "Bodeguero", "Cajero" });
            var outDTO = new ProductList();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _productManager.GetProductList();
            return outDTO;
        }


        public ProductList FindProduct(string productName, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador", "Bodeguero", "Cajero" });
            var outDTO = new ProductList();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _productManager.FindProduct(productName);
            return outDTO;
        }

        public Product FindProductById(int productId, string sessionId)
        {
            KillBillDTO sessionDTO = _securityService.IsAuthorized(sessionId, new string[] { "Administrador", "Bodeguero", "Cajero" });
            var outDTO = new Product();
            if (!sessionDTO.Success)
            {
                outDTO.Success = false;
                outDTO.Messages = sessionDTO.Messages;
            }
            else
                outDTO = _productManager.FindProductById(productId);
            return outDTO;
        }

    }

}
